/*- Coding With UTF-8 -*/

/*******************************************************************************
 *   File Name锛�     shell.h
 *   Description锛�   姝ゆ枃浠舵彁渚涗簡shell鐨勭浉鍏抽厤缃紝澹版槑
 *   Atuhor锛�        Letter
 *   Date:           2018/4/20
 *******************************************************************************/

#ifndef     __SHELL_H__
#define     __SHELL_H__

#include "stm32l4xx_hal.h"
#include "shellCmdList.h"

#define AW_SHELL

#ifdef AW_SHELL
/*------------------------------瀹忓畾涔�----------------------------------------*/
#define     SHELL_VERSION               "v1.8"                  //鐗堟湰
#define			SHELL_MODE_TERMINAL					0
#define			SHELL_MODE_SERIALPORT				1
#define			SHELL_MODE									SHELL_MODE_SERIALPORT

#define     SHELL_USE_HISTORY           1                       //鏄惁浣跨敤鍘嗗彶鍛戒护
#define     SHELL_ALLOW_SHIFT           1                       //鏄惁浣跨敤宸﹀彸閿Щ鍔ㄥ厜鏍�

//#define     shellUart                   USART1                //shell浣跨敤鐨勪覆鍙�
#define     shellUart                   huart1                //shell浣跨敤鐨勪覆鍙�

#define     SHELL_COMMAND_MAX_LENGTH    100                      //shell鍛戒护鏈�澶ч暱搴�
#define     SHELL_PARAMETER_MAX_LENGTH  50                     //shell鍛戒护鍙傛暟鏈�澶ч暱搴�
#define     SHELL_PARAMETER_MAX_NUMBER  10                       //shell鍛戒护鍙傛暟鏈�澶ф暟閲�

#define     SHELL_HISTORY_MAX_NUMBER    5                       //鍘嗗彶鍛戒护璁板綍鏁伴噺

#define     SHELL_COMMAND               "\r\nalwhales>>"

/*---------------------------鍑芥暟鎸囬拡瀹氫箟-------------------------------------*/
//typedef int (*shellFunction)();
typedef int (*shellFunction)(uint32_t argc, const char *argv[]);

/*----------------------------缁撴瀯浣撳畾涔�--------------------------------------*/
typedef struct
{
	const char *name;                                              //shell鍛戒护鍚嶇О
	shellFunction function;                                     //shell鍛戒护鍑芥暟
	const char *describe;                                          //shell鍛戒护鎻忚堪
} SHELL_CommandTypeDef;                                          //shell鍛戒护瀹氫箟

typedef struct
{
	uint8_t commandBuff[SHELL_COMMAND_MAX_LENGTH];
	uint8_t commandLength;                                      //鍛戒护闀垮害

#if SHELL_ALLOW_SHIFT == 1
	uint8_t commandCursor;                                      //鍏夋爣浣嶇疆
#endif

	char commandPara[SHELL_PARAMETER_MAX_NUMBER][SHELL_PARAMETER_MAX_LENGTH];
	uint32_t commandCount;
	const char *commandPointer[SHELL_PARAMETER_MAX_NUMBER];

#if SHELL_USE_HISTORY == 1
	uint8_t historyCommand[SHELL_HISTORY_MAX_NUMBER][SHELL_COMMAND_MAX_LENGTH];
	uint8_t historyCount;                                      //宸茶褰曠殑鍘嗗彶鍛戒护鏁伴噺
	int8_t historyFlag;                                         //褰撳墠璁板綍浣嶇疆
	int8_t historyOffset;
#endif

//    SHELL_CommandTypeDef *commandBase;
//    SHELL_CommandTypeDef *commandLimit;
} SHELL_TypeDef;

typedef enum{
	CONTROL_FREE = 0, CONTROL_STEP_ONE, CONTROL_STEP_TWO,
} CONTROL_Status;

/*-----------------------------鍑芥暟澹版槑---------------------------------------*/

uint8_t shellReceiveByte(void);                               //shell鎺ユ敹涓�瀛楄妭鏁版嵁

void shellDisplayByte(uint8_t data);

uint16_t shellDisplay(const char* string);                   //shell鏄剧ず涓�瀛楄妭鏁版嵁

void shellInit(void);                                           //shell鍒濆鍖�

void shellMain(void);                                        //shell涓诲嚱鏁帮紙闃诲寮忥級

void shellHandler_byte(uint8_t receiveByte);
uint16_t shellHandler_string(char *receiveString);

uint8_t shellStringCopy(uint8_t *dest, uint8_t *src);           //瀛楃涓插鍒�

void shellBackspace(uint8_t length);                            //shell閫�鏍�

#if SHELL_ALLOW_SHIFT == 1
void shellClearLine(void);                                    //shell娓呮鍛戒护琛屾搷浣�
#endif

#endif

#endif

